package demo.controllor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/test")
public class SessionSync {
	
	@Autowired
	 private Cache  ehCache;
	
	//.getAttribute("JSESSIONID")
	@RequestMapping(value = "/getSessionInCluster", method = RequestMethod.POST)
	@ResponseBody 
	public List<String> getSession(HttpServletRequest request){
		Cache s=ehCache.getCacheManager().getCache("SQL_CACHE");
		Object ss=request.getSession().getId();
		Element  el=s.get("session");
		if(el !=null){
			List<String> list=(List<String>)el.getValue();
			if(!list.contains(ss)){
				list.add(ss.toString());
			}
			return list;
		}else{
			List<String> mm=new ArrayList<String>();
			mm.add(ss.toString());
			 Element element = new Element("session", mm);       
		     s.put(element);  
		     return mm;
		}
		
	}

}
